package com.sangmo.fts.api.student;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.PageSearch;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.teacher.model.Examination;
import com.sangmo.fts.training.interfaces.TrainingEnterRequest;
import com.sangmo.fts.training.model.Training;
import com.sangmo.fts.training.service.TrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@Controller
@RequestMapping("/training/v1")
@Access("training")
public class TrainingApi  {

    @Autowired
    private TrainingService trainingService;


//    @Access(":read")
//    @RequestMapping(value = "/search",method = {RequestMethod.POST})
//    public Object search(@RequestBody @Valid PageSearch pageRequest) {
//        return trainingService.search(pageRequest);
//    }

//    @Access(disabled = true)
    @PostMapping("/{id}/read")
    public @ResponseBody ApiResponse<Training> read(@PathVariable("id") Long id) {
//        ExtViewHelper.setGroup("Examination.detail", 1);
        return ApiResponse.make(trainingService.find(id));
    }

//    @Access(disabled = true)
    @PostMapping("/search")
    public @ResponseBody ApiResponse<PageResult<Training>> search(@RequestBody @Valid DataViewPageSearch<HashMap> ps) {
//        ExtViewHelper.setGroup("Examination.search", 1);
        return ApiResponse.make(trainingService.search(ps));
    }

    @Access(":enter")
    @PostMapping(value = "/enter")
    public @ResponseBody
    ApiResponse<Training> enter(@RequestBody @Valid TrainingEnterRequest request) {
        ExtViewHelper.setGroup("default", 2);
        return ApiResponse.make(trainingService.enter(request));
    }

//    @Access(":rights")
    @Access(":enter")
    @PostMapping(value = "/{id}/rights")
    public @ResponseBody
    ApiResponse<Training> rights(@PathVariable("id") Long id) {
        return ApiResponse.make(trainingService.rights(id));
    }

}
